package com.hdoit.ares.logical.acc.dao.impl;

import java.util.List;

import org.springframework.stereotype.Component;

import com.alibaba.druid.util.StringUtils;
import com.hdoit.ares.logical.acc.dao.IGoldenBeanInfoDao;
import com.hdoit.ares.logical.acc.model.AccountInfo;
import com.hdoit.ares.logical.acc.model.GoldenBeanInfo;
import com.hdoit.ares.logical.viewmodel.acc.GoldenBeanInfoSearch;
import com.tennetcn.common.dao.base.IFinderExpression;
import com.tennetcn.common.dao.base.impl.SuperDao;
import com.tennetcn.common.enums.OrderEnum;
import com.tennetcn.common.message.PagerModel;
import com.tennetcn.common.utils.FinderExpressionFactory;
/**
 * @author      caochengmeng
 * @email       
 * @createtime  2017年12月05日 11:34:00
 * @comment
 */
@Component
public class GoldenBeanInfoDaoImpl extends SuperDao<GoldenBeanInfo> implements IGoldenBeanInfoDao{
	

	
	@Override
	public GoldenBeanInfo queryModel(String id){
		IFinderExpression sumfinderExpression = FinderExpressionFactory.createExpression();
		sumfinderExpression.select("sum(goldenBeanInfoa.promotionAward+goldenBeanInfoa.serviceAward+goldenBeanInfoa.hatcheryAward+"+
								"goldenBeanInfoa.firstSharingAward+goldenBeanInfoa.secondSharingAward) as sumMoney") 
						    .from(GoldenBeanInfo.class,"goldenBeanInfoa")
						    .andEq("goldenBeanInfo.id","goldenBeanInfoa.id");
		
		IFinderExpression finderExpression = FinderExpressionFactory.createExpression();
		finderExpression.select("goldenBeanInfo.*", "accountInfo.account as account","accountInfo.accountNo as accountNo",
								"("+sumfinderExpression.toSql()+") as sumMoney")
						.from(GoldenBeanInfo.class, "goldenBeanInfo")
						.leftJoin(AccountInfo.class, "accountInfo")
						.on("goldenBeanInfo.customerId", "accountInfo.customerId")
						.andEq("goldenBeanInfo.id", id);
		return super.queryModel(finderExpression);
	}
	
	@Override
	public List<GoldenBeanInfo> queryList(GoldenBeanInfoSearch incomeInfoSearch, PagerModel pagerModel) {
		IFinderExpression sumfinderExpression = FinderExpressionFactory.createExpression();
		sumfinderExpression.select("sum(goldenBeanInfoa.promotionAward+goldenBeanInfoa.serviceAward+goldenBeanInfoa.hatcheryAward+"+
								"goldenBeanInfoa.firstSharingAward+goldenBeanInfoa.secondSharingAward) as sumMoney") 
						.from(GoldenBeanInfo.class,"goldenBeanInfoa")
						.andEq("goldenBeanInfo.id","goldenBeanInfoa.id");
		
		
		IFinderExpression finderExpression = FinderExpressionFactory.createExpression();
		finderExpression.select("goldenBeanInfo.*", "accountInfo.account as account","accountInfo.accountNo as accountNo",
							"("+sumfinderExpression.toSql()+") as sumMoney")
						.from(GoldenBeanInfo.class, "goldenBeanInfo")
						.leftJoin(AccountInfo.class, "accountInfo")
						.on("goldenBeanInfo.customerId", "accountInfo.customerId")
						.addOrder("account", OrderEnum.asc);
		return queryList(finderExpression, incomeInfoSearch, pagerModel);
	}

	@Override
	public GoldenBeanInfo queryModelByCustomerId(String customerId) {
		IFinderExpression finderExpression=FinderExpressionFactory.createExpression();
		finderExpression.select("*")
						.from(GoldenBeanInfo.class)
						.andEq("customerId",customerId);
	    return queryModel(finderExpression);
	}

	@Override
	public List<GoldenBeanInfo> queryAllList(GoldenBeanInfoSearch search, PagerModel pagerModel) {
		
		IFinderExpression finderExpression=FinderExpressionFactory.createExpression();
		finderExpression.select("DISTINCT goldenBeanInfo.*","accountInfo.account as account","accountInfo.accountNo as accountNo","accountInfo.name as name")
						.from(GoldenBeanInfo.class,"goldenBeanInfo")
						.leftJoin(AccountInfo.class,"accountInfo")
						.on("goldenBeanInfo.customerId", "accountInfo.customerId")
						.setMainTableAlias("goldenBeanInfo");
		
		appendHoutai(search,finderExpression);
		
	    return queryList(finderExpression,search,pagerModel);
	}

	@Override
	public int queryAllCount(GoldenBeanInfoSearch search) {
		
		IFinderExpression finderExpression=FinderExpressionFactory.createExpression();
		finderExpression.select("count(DISTINCT goldenBeanInfo.id) as count")
						.from(GoldenBeanInfo.class,"goldenBeanInfo")
						.leftJoin(AccountInfo.class,"accountInfo")
						.on("goldenBeanInfo.customerId", "accountInfo.customerId")
						.setMainTableAlias("goldenBeanInfo");
		
		appendHoutai(search,finderExpression);
		
		return queryCount(finderExpression, search);
	}
	
	//后台的高级搜索
	private void appendHoutai(GoldenBeanInfoSearch search, IFinderExpression finder) {
		
		if(!StringUtils.isEmpty(search.getAccountNo())){
			finder.andWhere("accountInfo.accountNo like #{accountNo}")
			  	  .setParam("accountNo","%"+search.getAccountNo()+"%");
		}
		if(search.getAccount()!=null  ){
			finder.andWhere("accountInfo.account = #{account}")
			  	  .setParam("account",search.getAccount());
		}		
		if(!StringUtils.isEmpty(search.getName())){
			finder.andWhere("accountInfo.name like #{name}")
				  .setParam("name","%"+search.getName()+"%");
		}
		
	}

	@Override
	public GoldenBeanInfo queryThisModel(String id) {
		
		IFinderExpression finderExpression=FinderExpressionFactory.createExpression();
		finderExpression.select("DISTINCT goldenBeanInfo.*","accountInfo.account as account","accountInfo.accountNo as accountNo","accountInfo.name as name")
						.from(GoldenBeanInfo.class,"goldenBeanInfo")
						.leftJoin(AccountInfo.class,"accountInfo")
						.on("goldenBeanInfo.customerId", "accountInfo.customerId")
						.andWhere("goldenBeanInfo.id = #{id}")
						.setParam("id",id)
						.setMainTableAlias("goldenBeanInfo");
		
		return queryModel(finderExpression);
	}
}